<?php

/**
 * Description of JqGrid
 * Date Created Mar 25, 2012 9:44:43 PM
 * @package 
 * @author Bryan Salazar
 * 
 */
class JqGrid {
    public static function getJSON($total,$data,$idName='',$showID = false) {
        if(!is_array($data))
            return false;
        $jqgrid=array();
        $limit = $_GET['rows'];
        $jqgrid['page'] = $_GET['page'];
        $jqgrid['records'] = $total;
        
        $totalPage = ceil($total / $limit);
        
        $jqgrid['total'] = $totalPage;
        
//        $cntr=0;
        foreach($data as $d) {
            
            if($idName == '') {
                foreach($d as $k => $v) {
                    if($idName == '') {
                        $idName = $k;
                    }
                    break;
                } 
            }
            $id = $d[$idName];
            if(!$showID) {
                unset($d[$idName]);
            }
            
            $cell = array_values($d);
            
            $jqgrid['rows'][]=array('id'=>$id,'cell'=>$cell);
        }    
        return json_encode($jqgrid);
    }
}
